(*DONT_TOUCH = "TRUE"*)
module dds_ctrl #(
    parameter work_clk = 100_000_000,  // 100 MHz
    parameter ttl_freq = 1_000          // 1 kHz
)(
    input clk,
    input rst_n,
    output ttl, //原reg
    output wire [7:0] sin_ref,
    output wire [7:0] cos_ref
);

    // TTL 方波控制
    // localparam integer HALF_CYCLE = work_clk / (2 * ttl_freq);
    // reg [clogb2(HALF_CYCLE)-1:0] cnt;

    // always @(posedge clk or negedge rst_n) begin
    //     if (!rst_n) begin
    //         cnt <= 0;
    //         ttl <= 0;
    //     end else begin
    //         if (cnt == HALF_CYCLE - 1) begin
    //             cnt <= 0;
    //             ttl <= ~ttl;
    //         end else begin
    //             cnt <= cnt + 1;
    //         end
    //     end
    // end

    // DDS 输出接口
    wire [15:0] m_axis_data_tdata;
    wire m_axis_data_tvalid;
    wire m_axis_phase_tvalid;
    wire [27:0] m_axis_phase_tdata;
    // wire ttl_phase;
    assign ttl = m_axis_phase_tdata[27];

    dds_compiler u_dds_compiler (
        .aclk(clk),                              // input wire aclk
        .m_axis_data_tvalid(m_axis_data_tvalid),  // output wire m_axis_data_tvalid
        .m_axis_data_tdata(m_axis_data_tdata),    // output wire [31 : 0] m_axis_data_tdata
        .m_axis_phase_tvalid(m_axis_phase_tvalid),  // output wire m_axis_phase_tvalid
        .m_axis_phase_tdata(m_axis_phase_tdata)    // output wire [31 : 0] m_axis_phase_tdata
    );

    // 将 DDS 输出拆分为 sin 和 cos 分量（Q:sin，I:cos）
    assign sin_ref = m_axis_data_tdata[15:8];   // Q
    assign cos_ref = m_axis_data_tdata[7:0];  // I

function integer clogb2(input integer depth);
  integer tmp;
  begin
    tmp = depth;
    for (clogb2 = 0; tmp > 0; clogb2 = clogb2 + 1) 
      tmp = tmp >> 1;                          
  end
endfunction

endmodule
